package com.zznode.redsun.commons.sercurity.action;

import java.util.Collection;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.redsun.commons.sercurity.mapper.SecurityMapper;
import com.zznode.redsun.commons.sercurity.model.Account;
import com.zznode.redsun.commons.sercurity.model.Role;
import com.zznode.redsun.commons.sercurity.service.SecurityService;
import com.zznode.redsun.commons.utils.AbstractBaseAction;

@Controller
@ParentPackage("json-default")
@Namespace("/system/account")
public class AccountAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private SecurityService securityService;

	@Autowired
	private SecurityMapper securityMapper;

	@Autowired
	private EntityManager entityManager;

	private Account account;

	@Action(value = "index", results = { @Result(name = SUCCESS, location = "index.jsp") })
	public String index() {
		return SUCCESS;
	}

	@Action(value = "listAccounts", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_ROWS }) })
	public String listAccounts() {
		Collection<Account> accounts = this.securityMapper.findAllAccounts();
		this.fillJsonGridRows(accounts);
		return SUCCESS;
	}

	@Action(value = "addNew", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String addNew() {
		this.account = new Account();
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = SUCCESS, location = "edit.jsp") })
	public String edit() {
		String id = this.getParameter("id");
		if (StringUtils.hasText(id)) {
			this.account = entityManager.get(Account.class, Long.valueOf(id));
		} else {
			this.account = new Account();
		}
		return SUCCESS;
	}

	@Action(value = "save", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String save() {
		if (null != this.account) {
			this.securityService.saveAccount(account);
		} else {
			this.setJsonMessage("数据保存失败，提交数据为空！");
		}
		return SUCCESS;
	}

	@Action(value = "remove", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String remove() {
		String accountId = this.getParameter("id");
		if (StringUtils.hasText(accountId)) {
			if (Account.ADMIN_ID.equals(accountId)) {
				this.setJsonMessage("不允许删除系统管理员帐号");
			} else {
				this.entityManager.deleteById(Account.class, Long.valueOf(accountId));
			}
		} else {
			this.setJsonMessage("数据删除失败，提交数据为空！");
		}
		return SUCCESS;
	}

	@Action(value = "listRoles", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_ROWS }) })
	public String listRoles() {
		String accountId = this.getParameter("accountId");
		if (StringUtils.hasText(accountId)) {
			Collection<Role> roles = this.securityMapper.findAccountRoles(accountId);
			this.fillJsonGridRows(roles);
		}
		return SUCCESS;
	}

	@Action(value = "addRoles", results = { @Result(name = SUCCESS, location = "roles.jsp") })
	public String addRoles() {
		String accountId = this.getParameter("accountId");
		if (StringUtils.hasText(accountId)) {
			Collection<Role> roles = this.securityMapper.findChoosableRoles(accountId);
			this.putValueStack("roles", roles);
		}
		this.putValueStack("accountId", accountId);
		return SUCCESS;
	}

	@Action(value = "saveRoles", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String saveRoles() {
		String accountId = this.getParameter("accountId");
		String[] roleIds = (String[]) this.getValueStack("id");
		if (StringUtils.hasText(accountId) && ArrayUtils.isNotEmpty(roleIds)) {
			this.securityService.addRoles(Long.valueOf(accountId), roleIds);
		} else {
			this.setJsonMessage("数据删除失败，提交数据为空！");
		}
		return SUCCESS;
	}

	@Action(value = "removeRoles", results = { @Result(name = SUCCESS, type = JSON_TYPE, params = {
			JSON_PARAM_IGNOREHIERARCHY, "false", JSON_PARAM_INPROPERTIES, JSON_FLAG_MESSAGE }) })
	public String removeRoles() {
		String accountId = this.getParameter("accountId");
		String roleId = this.getParameter("roleId");
		if (StringUtils.hasText(accountId) && StringUtils.hasText(roleId)) {
			if (Account.ADMIN_ID.equals(accountId) && Role.ADMIN_ROLE_ID.equals(roleId)) {
				this.setMessage("不允许删除系统管理员的系统权限！");
			} else {
				this.securityService.removeRoles(Long.valueOf(accountId), roleId);
			}
		} else {
			this.setJsonMessage("数据删除失败，提交数据为空！");
		}
		return SUCCESS;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

}
